<?php
	/* -------------------------------------------------------------------
	IOU - expense sharing system 
	Copyright (C) 2009 - Mischa Spelt (m.spelt@gmail.com)
	
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
	
	------------------------------------------------------------------- */

	include_once("lib/lib.main.php");
	checklogin();
	print_header("Account details");

	if(isset($_POST['update'])) {
		$db->q("UPDATE Users SET Email = %s, DisplayName = %s, Account = %s, PreferredGroup = %i WHERE ID = %i", $_POST['email'], $_POST['displayname'], $_POST['account'], $_POST['prefgroup'], $gUser['ID']);
		print "Updated.";
	}
	if(isset($_POST['password'])) {
		if($db->q("value SELECT COUNT(*) FROM Users WHERE ID = %i AND Password = PASSWORD(%s)", $gUser['ID'], $_POST['old']) && $_POST['new'] == $_POST['cfm']) {
			$db->q("UPDATE Users SET Password = PASSWORD(%s) WHERE ID = %i", $_POST['new'], $gUser['ID']);
			print "Password changed. Please re-login.";

		}
		else
			print "Password has not been changed.";
	}

	if(isset($_POST['create'])) {
		include "lib/newuser.php";
		addUser($_POST['login'], $_POST['displayname'], $_POST['email'], $gUser['ID']);
	}
		
	$user = $db->q("tuple SELECT * FROM Users WHERE ID = %i", $gUser['ID']);
?>

<h1>Account details</h1>
<form method=post> 
<table> 
<tr class='gray'> <td>Username:</td> <td> <?php echo $user['Login']; ?> </td></tr> 
<tr> <td><label for='email'><span class='accesskey'>E</span>-mail address:</label></td> <td><input type='text' name='email' accesskey='E' value='<?php echo $user['Email']; ?>'><div class='hint'>Note: the e-mail address must be valid.</div></td></tr> 
<tr class='gray'> <td><label for='displayname' accesskey='D'><span class='accesskey'>D</span>isplayed name:</label></td> <td><input type='text' name='displayname' value='<?php echo $user['DisplayName']; ?>'></td></tr> 
<tr> <td><label for='account'><span class='accesskey'>A</span>ccount number:</label></td> <td><input type='text' name='account' accesskey='A' value='<?php echo $user['Account']; ?>'><div class='hint'>This will be shown to people who owe you.</div></td></tr> 
<tr> <td><label for='prefgroup'>Preferred <span class='accesskey'>g</span>roup:</label></td> <td><select name='prefgroup' id='prefgroup' accesskey='G'>
<?php
	foreach($db->q("table SELECT ID, DisplayName FROM Users_Groups LEFT JOIN Groups ON Users_Groups.GroupID = Groups.ID WHERE Users_Groups.UserID = %i ORDER BY DisplayName", $gUser['ID']) as $g) 
		print "<option value='{$g['ID']}'" . ($user['PreferredGroup'] == $g['ID'] ? " selected='selected'" : "") . ">{$g['DisplayName']}</option>";
?>
</select><div class='hint'>This group will be selected initially in forms.</div></td></tr>
<tr> <td colspan='2' style='text-align: center;'><input type='submit' name='update' accesskey='U' value='Update details'> <input type='reset' value='Reset'></td></tr> 
</table> 
</form> 

<h1>Change password</h1>
<form method=post> 
<table> 
<tr class='gray'> <td>Current:</td> <td> <input type='password' name='old'> </td> </tr>
<tr> <td>New:</td> <td> <input type='password' name='new' id='new1'  onchange="document.getElementById('changePassword').disabled = !(document.getElementById('new1').value == document.getElementById('new2').value);"> </td> </tr>
<tr class='gray'> <td>Confirm:</td> <td> <input type='password' name='cfm' id='new2' onchange="document.getElementById('changePassword').disabled = !(document.getElementById('new1').value == document.getElementById('new2').value);"> </td> </tr>
<tr> <td colspan='2' style='text-align: center;'><input type='submit' name='password' value='Change password' disabled='disabled' id='changePassword'> </td></tr> 
</table> 
</form> 

<h1>Create new user account</h1>
<form method=post> 
<table> 
<tr class='gray'> <td>Username:</td> <td> <input type='text' name='login'></td></tr> 
<tr> <td>E-mail address:</td> <td><input type='text' name='email'></td></tr> 
<tr class='gray'> <td>Displayed name:</td> <td><input type='text' name='displayname'></td></tr> 
<tr> <td colspan='2' style='text-align: center;'><input type='submit' name='create' value='Create a new user account'></td></tr> 
</table> 
</form> 

<?php
	print_footer();
?>
